using System;
using System.Collections.Generic;
using System.Linq;
using IBS.ServiceContracts;
using IBS.Bll.Accounts;
using IBS.Model;
using IBS.Model.Base;
using IBS.Bll;
using System.Threading;
using IBS.Bll.Tools;

namespace IBS.Bll.AutoPayments
{
    // Check autopayments each 6 hours:
    // there're two kinds of payments - 1) transfer a fixed sum on given day of the month
    //                                  2) transfer a fixed sum when the target account's balance is less than minimal
    public class AutoPaymentService : ServiceBase, IAutoPaymentService
    {       
        private IPaymentService _paymentService;
        private readonly ITransferService _transferService;
        private readonly ICardService _cardService;

        public AutoPaymentService(IIbsContext context,  IPaymentService paymentService, ITransferService transferService, ICardService cardService)
            : base(context)
        {
            _paymentService = paymentService;
            _transferService = transferService;
            _cardService = cardService;
        }

        public IList<AutoPayment> GetAutoPayments(Func<AutoPayment, bool> criteria = null, int?page = null )
        {
           var result = _context.AutoPayments.GetByCriteria(page, a => !a.isDeleted && criteria(a));
            return result;
        }

        public AutoPayment CreateAutoPayment( int  paymentId,string paymentDetail, string fromCardNumber, int dayOfMonth, decimal amount,  string ISO)
        {
            var fromCard = _cardService.GetCardByNumber(fromCardNumber);
            Payment payment = _paymentService.GetPaymentById(paymentId);
            var autoPayment = new AutoPayment()
                                  {
                                      Card = fromCard,
                                      Payment = payment,
                                      DayOfMonth = dayOfMonth,
                                      PaymentDetail = paymentDetail,
                                      Amount = amount,
                                      ISO = ISO
                                  };
            _context.AutoPayments.AddObject(autoPayment);
            _context.SaveChanges();
            return autoPayment;
        }
        public AutoPayment GetAutoPaymentById(int autoPaymentId)
        {
            try
            {
                return _context.AutoPayments.Single(a => a.Id == autoPaymentId && !a.isDeleted);
            }
            catch (InvalidOperationException)
            {
                throw new BusinessException("Autopayment does not exist");
            }
        }

        public void DeleteAutoPayment(int autoPaymentId)
        {
            var autoPayment = GetAutoPaymentById(autoPaymentId);
            autoPayment.isDeleted = true;
            _context.SaveChanges();
        }

        public void UpdateAutoPayment(AutoPayment autoPayment)
        {
            _context.SaveChanges();
        }
    }
}

